package com.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.controller.common.AddressParam;
import com.controller.reqdto.AddAddressParam;
import com.controller.reqdto.AddressQueryParam;
import com.controller.reqdto.AddressUpdateParam;
import com.controller.respdto.AddressList;
import com.repository.mapper.AddressMapper;
import com.repository.mapper.PcMessageMapper;
import com.repository.po.Address;
import com.repository.po.PcMessage;
import com.service.AddressService;

import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author Ruiwen
 * @since 2024-11-19
 */
@Service
public class AddressServiceImpl implements AddressService {
    @Resource
    AddressMapper addressMapper;
    @Resource
    PcMessageMapper pcMessageMapper;


    @Override
    public AddressList getAddressList() {
        List<Address> addresses = addressMapper.selectList(null);
        if (addresses == null || addresses.size() == 0) {
            return AddressList.builder().addressList(null).build();
        }
        List<AddressParam> addressParams = new ArrayList<>();
        addresses.iterator().forEachRemaining(address -> {
            AddressParam addressParam = new AddressParam();
            BeanUtils.copyProperties(address, addressParam);
            addressParams.add(addressParam);
        });
        return AddressList.builder().addressList(addressParams).total((long) addressParams.size()).build();
    }

    @Override
    public AddressList queryAddressList(AddressQueryParam queryParam) {
        Page<Address> page = new Page<Address>(queryParam.getPage(), queryParam.getLimit());
        QueryWrapper<Address> queryWrapper = new QueryWrapper<Address>();
        if (Objects.nonNull(queryParam.getName())) {
            queryWrapper.like("name", queryParam.getName());
        }
        if (Objects.nonNull(queryParam.getAddress())) {
            queryWrapper.like("address", queryParam.getAddress());
        }
        if (Objects.nonNull(queryParam.getChargeName())) {
            queryWrapper.like("charge_name", queryParam.getChargeName());
        }
        if (Objects.nonNull(queryParam.getChargePhone())) {
            queryWrapper.eq("charge_phone", queryParam.getChargePhone());
        }
        Page<Address> addressPage = addressMapper.selectPage(page, queryWrapper);
        List<Address> records = addressPage.getRecords();
        if (records == null || records.size() == 0) {
            return AddressList.builder().addressList(null).total(0L).build();
        }
        List<AddressParam> addressParams = new ArrayList<>();
        records.iterator().forEachRemaining(address -> {
            AddressParam addressParam = new AddressParam();
            BeanUtils.copyProperties(address, addressParam);
            addressParams.add(addressParam);
        });
        return AddressList.builder().addressList(addressParams).total(addressPage.getTotal()).build();
    }

    @Override
    public Boolean updateAddressMsg(AddressUpdateParam updateParam) {
        Address address = new Address();
        BeanUtils.copyProperties(updateParam, address);
        return addressMapper.updateById(address) > 0;
    }

    @Override
    public Boolean deleteAddress(Integer id) {
        if (pcMessageMapper.selectList(new QueryWrapper<PcMessage>().eq("address_id", id)).size() > 0) {
            throw new RuntimeException("该场地下存在宿主机，无法删除");
        }
        return addressMapper.deleteById(id) > 0;
    }

    @Override
    public Boolean addAddress(AddAddressParam addAddress) {
        List<Address> addresses = addressMapper.selectList(new QueryWrapper<Address>().eq("name", addAddress.getName()));
        if (addresses.size() > 0) {
            throw new RuntimeException("该场地已存在");
        }
        Address address =new  Address();
        BeanUtils.copyProperties(addAddress, address);
        return addressMapper.insert(address) > 0;
    }
}
